package com.adlotto2.lcdns.activity.info.adapter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.adlotto2.lcdns.R;
import com.adlotto2.lcdns.cronos.data.CNLottoAdAdressData;
import com.adlotto2.lcdns.cronos.datahandler.CNBaseDataSet;
import com.adlotto2.lcdns.util.CNCronosLog;
import com.adlotto2.lcdns.util.RecycleUtils;

/**
 * 주소 Adapter
 * @author Administrator
 *
 */
public class LottoAdAdressAdapter extends BaseAdapter{

	protected LayoutInflater mInflater = null;
	protected ArrayList<CNBaseDataSet> mDataSetList = null;
	
	private List<WeakReference<View>> mRecycleList = new ArrayList<WeakReference<View>>();
	
	public void recycle() {
		for (WeakReference<View> ref : mRecycleList) {
			RecycleUtils.recursiveRecycle(ref.get());
		}
	}
	
	private class ViewHolder {
		TextView mAdressTv = null;
	}
	
	public LottoAdAdressAdapter(Context context){
		this.mInflater = LayoutInflater.from(context);
	}
	
	public void setDataSetList(ArrayList<CNBaseDataSet> dataSetList) {
		this.mDataSetList = dataSetList;
	}
	
	public void clear(){
		if(this.mDataSetList != null){
			this.mDataSetList.clear();
			this.notifyDataSetChanged();
		}
	}
	
	@Override
	public int getCount() {
		if (this.mDataSetList != null) {
			return this.mDataSetList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (this.mDataSetList != null) {
			return this.mDataSetList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			convertView = this.mInflater.inflate(R.layout.lottoad_info_adress_low, null);
			holder = new ViewHolder();
			
			holder.mAdressTv = (TextView) convertView.findViewById(R.id.textView_adress);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		CNLottoAdAdressData dataSet = (CNLottoAdAdressData) getItem(position);
		
		if(dataSet != null){
			if(dataSet.adressSIDO != null && dataSet.adressSIDO.length() > 0){
				holder.mAdressTv.setText(dataSet.adressSIDO);
			}
			else if (dataSet.adressGUGUN != null && dataSet.adressGUGUN.length() > 0){
				CNCronosLog.d("getView : dataSet.adressGUGUN===========>" + dataSet.adressGUGUN);
				if(dataSet.adressGUGUN.length() > 7){
					holder.mAdressTv.setTextSize(18);
				} 
				holder.mAdressTv.setText(dataSet.adressGUGUN);
			}
			else if (dataSet.adressDONG != null && dataSet.adressDONG.length() > 0){
				if(dataSet.adressDONG.length() > 7){
					holder.mAdressTv.setTextSize(18);
				} 
				holder.mAdressTv.setText(dataSet.adressDONG);
			}
			holder.mAdressTv.setSelected(true);
			holder.mAdressTv.setPaintFlags(holder.mAdressTv.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG);
		}
		
		return convertView;
	}
}
